﻿@{
    var languages = ViewBag.AvailableLanguages as IList<LanguageModel>;
    
    if (languages.Count <= 1)
    {
        return;
    }

    var activeLang = languages.FirstOrDefault(x => x.Id == WorkContext.WorkingLanguage.Id) ?? languages.FirstOrDefault();
}

<div class="dropdown language-selector">
    <a class="menubar-link" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" href="#">
        <span>@activeLang.CultureCode.ToUpper()</span>
        <i class="fa fa-angle-down"></i>
    </a>
    <div class="dropdown-menu" aria-labelledby="dLabel">
        @foreach (var lang in languages)
        {
            var returnUrl = ViewBag.ReturnUrls[lang.CultureCode] as string;
            var href = Display.ModifyQueryString(Url.RouteUrl("ChangeLanguage", new { langid = lang.Id }), "returnUrl=" + returnUrl.UrlEncode(), null);

            <a href="@href" title="@lang.Name (@lang.CultureCode.ToUpper())" rel="nofollow" class="dropdown-item" attr-class='(lang.Id == activeLang.Id, "disabled")'
               data-selected="@(lang.Equals(activeLang) ? "true" : "false")" data-abbreviation="@lang.Name">
                @if (ViewBag.UseImages == true)
                {
                    <img src="@Url.Content("~/images/flags/" + lang.FlagImageFileName)" alt="@lang.FlagImageFileName" />
                }
                <span>@(ViewBag.DisplayLongName ? lang.NativeName : lang.ShortNativeName)</span>
            </a>
        }
    </div>
</div>

<script sm-target-zone="scripts" data-origin="language-selector">
    $(function () {
        $(".language-selector .dropdown-item.disabled").on('click', function (e) {
            e.preventDefault();
        });
    });
</script>